package com.bupt.kgplatform.controller.docker;

import com.bupt.kgplatform.Util.R;
import com.bupt.kgplatform.entity.DataCleanTask;
import com.bupt.kgplatform.service.DataCleanTaskService;
import com.bupt.kgplatform.service.DockerService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * DockerController
 *
 * @author zrz
 * @date 2023/5/26
 * @time 16:35
 * @Description docker
 */

@RestController
@RequiredArgsConstructor
@RequestMapping("/docker" )
@Api(value = "docker", tags = "容器")
public class DockerController {

    private final DockerService dockerService;
    private final DataCleanTaskService dataCleanTaskService;

    @GetMapping("/start")
    public R startDocker(@Param("id") String id){
        R Result =  dockerService.restartContainer(id);
        if (Result.getCode() == 1){
            DataCleanTask byId = dataCleanTaskService.getById(id);
            byId.setDockerState("success");
            dataCleanTaskService.updateById(byId);
            return Result;
        }
        else {
            return Result;
        }
    }


    @GetMapping("/stop")
    public R stopDocker(@Param("id") String id){
        R Result = dockerService.stopContainer(id);

        if (Result.getCode() == 1){
            DataCleanTask byId = dataCleanTaskService.getById(id);
            byId.setDockerState("failed");
            dataCleanTaskService.updateById(byId);
            return Result;
        }
        else {
            return Result;
        }
    }





}
